//
//  LMNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMNetWorkRequestManager.h"

@interface LMNetWorkRequestManager ()

@end


@implementation LMNetWorkRequestManager
#pragma mark - 单利方法

//+ (instancetype)defaultManager{
//    static LMNetWorkRequestManager *manager = nil;
//    @synchronized(self) {
//        if (!manager) {
//            manager = [[LMNetWorkRequestManager alloc]init];
//        }
//    }
//    return manager;
//}



#pragma mark - 请求方法
+(void)requestURL:(NSURL *)url Method:(URLRequestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock{
    ///空的可变字符串
    NSMutableString  *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
//        拼接url
      [urlString appendString:(NSString *)url.absoluteString];
    }else if ([url isKindOfClass:[NSString class]]){
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length == 0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reason":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        return;
    }
    //把参数字典转成字符串
//    LMNetWorkRequestManager *manager = [LMNetWorkRequestManager defaultManager];
    
//    现在不用单利也可以了
    LMNetWorkRequestManager *manager = [LMNetWorkRequestManager new];
    
    NSString *paraStr = [manager strFromDict:param];
    
    switch (method) {
        case LM_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
            }
            [manager requestURL:[NSURL URLWithString:urlString] ResultBlock:resultBlock];
        }
            break;
        case LM_POST:
            [manager postURL:urlString ParaStr:paraStr  ResultBlock:resultBlock];
            break;
        default:
            break;
    }
}


#pragma mark - GET网络请求
- (void)requestURL:(NSURL *)url ResultBlock:(ResultBlock )block{
//    在Block中注意循环引用问题:用下面这个修饰self
//    __weak typeof(self) weakSelf  = self;
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:block];
    //开始网络请求
    [dataTask resume];
}

#pragma mark - POST网络请求
- (void)postURL:(NSString *)urlStr ParaStr:(NSString *)paraStr ResultBlock:(ResultBlock )block{
//    创建URL
    NSURL *url = [NSURL URLWithString:urlStr];
//    创建可变的网络请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
//    设置POST
    [mRequest setHTTPMethod:@"POST"];
//    请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:block];
    [dataTask resume];
}

#pragma mark - 把参数字典转成字符串
- (NSString *)strFromDict:(NSDictionary *)dict{
//    空数组
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
//    数组拼接字符串
    NSString *para  = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
//    返回
    return para;
}

















@end
